package com.cuitrip.business.date.ui;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.cuitrip.business.home.arrange.model.MonthCalendar;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.time.TimeUtil;
import com.lab.adapter.BaseHolderView;
import com.lab.adapter.IAdapterData;
import com.lab.widget.CtGridView;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ak on 15/10/16.
 */
public class MonthSelectHolderView extends BaseHolderView {

    private MonthSelectDateAdapter monthDateAdapter;

    @Bind(R.id.month_title)
    TextView monthView;

    @Bind(R.id.date_week_grid)
    CtGridView gridView;

    private boolean selectAll;

    private TimeZone serviceTimeZone;

    public MonthSelectHolderView(Context context) {
        super(context, R.layout.item_month_date_view);
    }

    private MonthSelectDateAdapter.OnSelectChangeCallBack onSelectChangeCallBack;

    public void setOnSelectChangeCallBack(MonthSelectDateAdapter.OnSelectChangeCallBack onSelectChangeCallBack){
        this.onSelectChangeCallBack = onSelectChangeCallBack;
    }

    @Override
    public void initView(View rootView) {
        this.setClickable(true);
        ButterKnife.bind(this);
        monthDateAdapter = new MonthSelectDateAdapter(mContext);
        gridView.setAdapter(monthDateAdapter);
        monthDateAdapter.setOnSelectChangeCallBack(new MonthSelectDateAdapter.OnSelectChangeCallBack() {
            @Override
            public void onSelect(Long time) {
                if (onSelectChangeCallBack != null){
                    onSelectChangeCallBack.onSelect(time);
                }
            }

        });
    }

    public void setServiceTimeZone(TimeZone serviceTimeZone) {
        this.serviceTimeZone = serviceTimeZone;
    }

    public void setSelectAll(boolean selectAll) {
        this.selectAll = selectAll;
    }

    public void setBookDateList(List<Long> bookDateList) {
        monthDateAdapter.setBookDateList(bookDateList);
    }

    public void setAvailableDateList(List<Long> availableDateList) {
        monthDateAdapter.setAvailableDateList(availableDateList);
    }

    public void setSelectDate(long selectDate){
        monthDateAdapter.setSelectDate(selectDate);
    }


    @Override
    public void bindData(final IAdapterData data, final int position) {
        if (data instanceof MonthCalendar) {
            MonthCalendar monthCalendar = (MonthCalendar) data;
            monthDateAdapter.setCalendar(monthCalendar.getCalendar());
            Calendar calendar = Calendar.getInstance(I118Util.getLocale());
            calendar.set(monthCalendar.getCalendar().get(Calendar.YEAR),monthCalendar.getCalendar().get(Calendar.MONTH),1);
            monthView.setText(TimeUtil.formatDay_YM(monthView.getContext(),calendar.getTimeInMillis()));
            monthDateAdapter.notifyDataSetChanged();
        }
    }

}
